<?php
// +----------------------------------------------------------------------
// | 小说系统 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------

namespace app\common\controller;

class MobileFornt extends Base {

	public function _initialize() {
		parent::_initialize();

		

		//判读是否为关闭网站
		if (\think\Config::get('web_site_close')) {
			header("Content-type:text/html;charset=utf-8");
			echo $this->fetch('common@default/public/close');exit();
		}
		$host = explode('.',$_SERVER['HTTP_HOST']);
		if(strpos($host[0],'t') !==false){
			$agentid = split_host($host[0]);
			$wxmpid= split_host($host[0],2);
			cookie('wxmpid', $wxmpid,['path'=>'/','expire'=>86400,'domain'=>DOMAIN]);
			cookie('agentid', $agentid,['path'=>'/','expire'=>86400,'domain'=>DOMAIN]);
			$this->param["setthemes"] = 'yes';
		}	
			
		//二维码访问
		$request = request();
		if(config('open_mobile_site')==1&&strpos($request->url(true),"/login/qrcode")===false){
			if($this->isMobile()===false&&$this->is_wechat()===false || $this->isMobile()&&$this->is_wechat()===false){
				$this->redirect('/login/qrcode?jumpurl='.urlencode($request->url(true)));
			}
		}
		
		//设置SEO
		$this->setSeo();

		//主题设置
		$this->setThemes();

		//设置客服信息
		$this->setKefu();

		//设置弹窗信息
		$this->setPopup();
	}

	protected function setThemes() {
		//网站主题设置

		//$themes['mobile'] = config('mobile_themes') ? config('mobile_themes') : 'mobile';
		
		if(array_key_exists('setthemes',$this->param)){
			if(array_key_exists('themes',$this->param)){
				$themes['mobile']=$this->param['themes'];
			}else{
				$mobile_themes=db('wxmp')->where('id',cookie('wxmpid'))->value('mobile_themes');
				$themes['mobile']=$mobile_themes;
			}
		}else{
			if(array_key_exists('themes',$this->param)){
				$themes['mobile']=$this->param['themes'];
			}else{
				$themes['mobile']=config('mobile_themes');
			}
		}

		session('mobile_themes',$themes['mobile']);

		$view_path        = 'template/' . $themes['mobile'] . '/';
		
		$module = $this->request->module();
		if (!in_array($module, array( 'install'))) {
			$view_path_pre = $module . '/';
		} else {
			$view_path_pre = '';
		}
		
		$this->view->config('view_path', $view_path . $view_path_pre)
			->config('tpl_replace_string',array(
				'__IMG__' => BASE_PATH . '/' . $view_path . 'static/images',
				'__JS__'  => BASE_PATH . '/' . $view_path . 'static/js',
				'__CSS__' => BASE_PATH . '/' . $view_path . 'static/css',
			));
	}

	protected function setKefu(){
		$host = explode('.',$_SERVER['HTTP_HOST']);
		if(strpos($host[0],'t') !==false){
			$wxmpid= split_host($host[0],2);
			$kfinfo = db('wxmp')->where(['id'=>$wxmpid])->field('nickname,subscribe_url')->find();
			
		}else{
			$kfinfo = db('wxmp')->where(['id'=>1])->field('nickname,subscribe_url')->find();
		}
		$this->assign("kfinfo",$kfinfo);
	}

	public function setPopup() {
		$host = explode('.',$_SERVER['HTTP_HOST']);
		$activity = array();
		if(strpos($host[0],'t') !==false){
			$uid= split_host($host[0],0);
			$wxmpid= split_host($host[0],2);
			$time = time();
			if(session('user_auth.uid')>0){
				
				$activity = db('promotion')->alias('p')->join('cps_pay_setting s','s.id = p.payid')->where(['p.wxmpid'=>$wxmpid,'p.uid'=>$uid,"p.is_diy"=>1])->where("p.starttime <= $time and p.endtime > $time")->order('p.starttime desc,p.id desc')->field('p.*,s.price,s.egold,s.giveegold,s.type,s.description')->find();
				//渠道自定义活动为空则获取默认平台自定义活动
				if(empty($activity)){
					$activity = db('promotion')->alias('p')->join('cps_pay_setting s','s.id = p.payid')->where(['p.wxmpid'=>0,'p.uid'=>1,"p.is_diy"=>1,'p.template_id'=>array("in","1,2,3")])->where("p.starttime <= $time and p.endtime > $time")->order('p.starttime desc,p.id desc')->field('p.*,s.price,s.egold,s.giveegold,s.type,s.description')->find();
				}
				
				if($activity){
					
					if($activity["user_limit"]){
						$paylimit = db('order')->where("uid",session('user_auth.uid'))->where("status",1)->where("activityid",$activity['id'])->count();
						//大于限购次数则不显示
						if($paylimit>$activity["user_limit"]){
							$activity = array();
						}
					}
					
					$paynum = db('member')->where("uid",session('user_auth.uid'))->value('pay');
					
					if($activity && $activity["user_type"]==1&&$paynum<=0){//不是已充值用户
						$activity = array();
					}
					
					if($activity &&  $paynum>0&&$activity["user_type"]==2){//不是未充值用户
						$activity = array();
					}
					
					if($activity && $activity["popup_enabled"]==0){
						$activity = array();
					}
				}
			}
			
		}/*else{
			$activity = db('promotion')->where(['uid'=>1,"is_diy"=>1])->where("starttime <= $time and endtime > $time")->find();
		}*/
		
		$this->assign("activity",$activity);
	}
}
